package src.com.qf.dao.impl;


import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import src.com.qf.dao.GoodDao;
import src.com.qf.entity.Good;
import src.com.qf.entity.Page;
import src.com.qf.utils.JdbcUtil;

import java.sql.SQLException;
import java.util.List;

public class GoodDaoImpl implements GoodDao {
    QueryRunner queryRunner = new QueryRunner();
    @Override
    public int selectCount(Integer typeId)  {
        String sql = "select count(*) from tb_good where typeId=?";
        long query = 0;
        try {
            query = (long)queryRunner.query(JdbcUtil.getCon(), sql, new ScalarHandler(), typeId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return (int)query;
    }

    @Override
    public List<Good> selectLitmitGoodsByTypeId(Integer typeId, Page page) {
        String sql = "select *from tb_good where typeId=? limit ?,?";
        List<Good> query =null;
        try {
            query = queryRunner.query(JdbcUtil.getCon(), sql, new BeanListHandler<>(Good.class), typeId, page.getStartRows(), page.getPageSize());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return query;
    }

    @Override
    public Good selectGoodById(int id) {
        String sql = "select *from tb_good where id = ?";
        Good query = null;
        try {
            query = queryRunner.query(JdbcUtil.getCon(), sql, new BeanHandler<>(Good.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }
}
